package com.example.translateappjava.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.example.translateappjava.R;

public class WelcomeActivity extends AppCompatActivity {

    private TextView tvCountdown;
    private CountDownTimer countDownTimer;
    private long timeLeftInMillis = 3000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        MainActivity.setBackGroundColor(WelcomeActivity.this);
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_welcome);

        // 正确地初始化tvCountdown
        tvCountdown = findViewById(R.id.Time_3s);

        startCountdown();
    }

    private void startCountdown() {
        countDownTimer = new CountDownTimer(timeLeftInMillis, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                timeLeftInMillis = millisUntilFinished;
                int secondsRemaining = (int) (millisUntilFinished / 1000);
                tvCountdown.setText(secondsRemaining + " s");
            }

            @Override
            public void onFinish() {
                // 跳转到登录页面
                startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
                // 倒计时结束后的操作，例如跳转到主页面
                finish();
            }
        }.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }
}
